#include "gui/VisualComponents/der/VisualComponentViaDER.h"
#include "gui/VisualComponents/VisualComponentConector.h"
#include "gui/VisualComponents/VisualComponentSegment.h"
#include "model/Utils.h"
#include "model/der/Fork.h"
#include "gui/SubWindow/ViewModifyComponentInfoWin.h"
#include "gui/VisualComponents/ArrowStyleCreator.h"

VisualComponentViaDER::VisualComponentViaDER(Diagram* diagram) : VisualDiagramComponent(diagram), VisualComponentVia(diagram), VisualDERComponent(diagram) {
	m_component = new ViaDER();
	setStartConector(NULL);
	setEndConector(NULL);
	initialize();
}

VisualComponentViaDER::VisualComponentViaDER(VisualComponentConector* start, Diagram* diagram, VisualComponentConector* end) :
		VisualDiagramComponent(diagram), VisualComponentVia(start, diagram, end), VisualDERComponent(diagram) {
	m_component = new ViaDER();
	setStartConector(start);
	setEndConector(end);
	m_component->setX(m_startConector->getX());
	m_component->setY(m_startConector->getY());
	ArrowStyleCreator::setArrowStyle(ArrowHead::ARROW_LINE);
	createSegments();
	initialize();
}

VisualComponentViaDER::VisualComponentViaDER(const VisualComponentViaDER& other) :
			VisualDiagramComponent(other.m_diagram), VisualComponentVia(other.m_diagram), VisualDERComponent(other.m_diagram) {
	m_component = new ViaDER(*dynamic_cast<ViaDER*>(other.m_component));
	initialize();
	initializeSegments();
}

VisualComponentViaDER::~VisualComponentViaDER() {

}


void VisualComponentViaDER::showProperties(){
	if(m_startConector->isEditable() && m_endConector->isEditable()) {
		ViewModifyComponentInfoWin dialog(getDERComponent());
		dialog.run();
	}
	return;
}

void VisualComponentViaDER::load(XmlReader& reader, Diagram* diagram){
	VisualDERComponent::load(reader, reader, diagram);
	this->initializeSegments();
}

void VisualComponentViaDER::load(XmlReader& reader, XmlReader & readerModel, Diagram* diagram){
	VisualDERComponent::load(reader, reader, diagram);
	this->initializeSegments();
}

VisualDERComponent* VisualComponentViaDER::getCopy(Diagram* diagram){
	Diagram* origen = m_diagram;
	m_diagram = diagram;
	VisualDERComponent* comp = new VisualComponentViaDER(*this);
	m_diagram = origen;
	return comp;
}

void VisualComponentViaDER::draw(Cairo::RefPtr<Cairo::Context> ptrContext){
	ViaDER* via = dynamic_cast<ViaDER*>(m_component);
	ptrContext->save();

	VisualDiagramComponent::draw( ptrContext);
	Draw draw;

	std::list<Point *>::iterator i;
	for(i=via->getPoints().begin(); i != via->getPoints().end(); i++) {
		Point* point1 = (*i);
		i++;
		Point* point2 = (*i);
//		std::cout << point1 << " " << point2 << std::endl;

		draw.writeTextInContext(ptrContext,  (point1->getX() + point2->getX() ) / 2 + 14, ( point1->getY() + point2->getY() ) / 2 - 2, via->getTypes()[via->getType()], COLOR_IN_TEXT);
		if(via->getName() != "rol")
			draw.writeTextInContext(ptrContext,  (point1->getX() + point2->getX() ) / 2 + 18,
					( point1->getY() + point2->getY() ) / 2 + 12 , via->getName(), COLOR_IN_TEXT);
	}
}
